package com.wangtianxiang.sxp.module.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangtianxiang.sxp.common.constant.WxUActTypeConst;
import com.wangtianxiang.sxp.common.dto.PageData;
import com.wangtianxiang.sxp.dto.ActivityPageDto;
import com.wangtianxiang.sxp.po.Activity;
import com.wangtianxiang.sxp.po.WxUAct;
import com.wangtianxiang.sxp.module.dao.ActivityMapper;
import com.wangtianxiang.sxp.module.service.ActivityService;
import com.wangtianxiang.sxp.module.service.WxUActService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private WxUActService wxUActService;

    @Override
    @Transactional(transactionManager = "baseTransactionManager",propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class)
    public void saveActivity(Activity activity) throws Exception {
        this.save(activity);
        wxUActService.save(new WxUAct().setUserId(activity.getActivityUserId()).setActivityId(activity.getId()).setType(WxUActTypeConst.PUBLISH));
    }

    @Override
    public PageData<Activity> pageList(ActivityPageDto dto) throws Exception {
         return new PageData<Activity>(dto.getPage(),dto.getPageSize(),activityMapper.actCount(dto.getType()),
                 activityMapper.actList(dto.getType(),dto.getStartIndex(),dto.getPageSize()));
    }
}
